import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.TreeMap;

public class Solution1944Bin {
    public static void main(String[] args) {
        int[] heights = {10,6,8,5,11,9};
        // int[] heights = {5,1,2,3,10};
        // int[] heights = {100000,99998,99996,99994,99992,99991,99990};
        int[] res = Solution1944Bin.canSeePersonsCount(heights);
        System.out.println(Arrays.toString(res));
    }

    public static int[] canSeePersonsCount(int[] heights) {
        int n = heights.length;
        int[] res = new int[n];

        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = n-1; i >=0; i--) {
            while (!stack.isEmpty() && stack.peek() < heights[i]){
                stack.pop();
                res[i]++;
            }
            if (!stack.isEmpty()){
                res[i]++;
            }
            stack.push(heights[i]);
        }
        return res;
    }
}
